<?php
use think\Db;
require_once '../extend/getid3/getid3.php';

function curl_post($url, $data = array())
{

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    // POST数据

    curl_setopt($ch, CURLOPT_POST, 1);

    // 把post的变量加上

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $output = curl_exec($ch);

    curl_close($ch);

    return $output;

}

function get_file_size($file,$type ='playtime_seconds'){



    if(file_exists($file)) {
        $mp3 = new \getID3();

        $info =  $mp3->analyze($file);

        //print_r($info);
        if(isset($info[$type])) {
            $time = (int)ceil($info[$type]);
        }else{
            $time = 0;
        }
        return $time;

    }

}

function changeS($seconds)
{
    $hours = floor($seconds / 3600);
    $mins = floor($seconds / 60 % 60);
    $secs = floor($seconds % 60);
    $timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
    return $timeFormat;
}


function getUser($token,$key){
    $where['token'] = $token;
    $result =  Db::name("user")->where($where)->find();

    if($key){
        return $result[$key];
    }
    return $result;
}


function fileUpload($url, $post_data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_URL, $url);
    $info = curl_exec($ch);
    curl_close($ch);
    return $info;

}
function randomkeys($length)
{
    $key = '';
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz 
               ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
    for($i=0;$i<$length;$i++)
    {
        $key .= $pattern{mt_rand(0,35)};    //生成php随机数
    }
    return $key;
}

function returnRst($data=[],$msg='success',$code=200)
{
    $returnRst['data'] = $data;
    $returnRst['msg'] = $msg;
    $returnRst['code'] = $code;
    exit(json_encode($returnRst));
}

function p($data)
{
    echo "<pre>";
    print_r($data);
    exit;
}
